package com.lvmama.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
public class ValidationController {

	@Autowired
	private JedisPool jedisPool;

	@Value("${tokenTimeout}")
	private Integer tokenTimeout;
	
	@RequestMapping(value = "/validatetoken" ,method = RequestMethod.GET)
	public String validateToken(HttpServletRequest request, HttpServletResponse response
			){
		String token = request.getParameter("token");
		
		Jedis jedis = this.jedisPool.getResource();
		if(jedis.get(token.getBytes()) != null) {
			//验证成功，延长token有效时间
			jedis.expire(token.getBytes(), tokenTimeout);
			jedis.close();
			return token;
		}
		jedis.close();
		
		return null;
	}
}
